package com.apestech.framework.run.core;

import java.util.Hashtable;

public class SingleInstance {
	
	private static Hashtable<String, Object> SYS_INSTANCE=new Hashtable<String, Object>();
	
	public static Object getInstance(String className) throws Exception{
		Object instance=SYS_INSTANCE.get(className);
		if(instance==null){
			instance=getLazyInstance(className);
			SYS_INSTANCE.put(className, instance);
		}
		return instance;
	}
	
	private static Object getLazyInstance(String className) throws Exception {
		Object instance = null;
    	try {    
    		//创建实例之前可能会有一些准备性的耗时工作   
            Thread.sleep(300);  
            Class classType = Class.forName(className);
            synchronized (classType) {  
                if(SYS_INSTANCE.get(className) == null){//二次检查  
                	instance=classType.newInstance();  
                }  
            }   
        } catch (InterruptedException e) {   
        	throw new Exception(e.getMessage());
        }  
        return instance;  
    }

}
